﻿using System.Collections.Generic;
using System.Data;
using System.Linq;
using QuanLyHopDong.Biz.Model;

namespace QuanLyHopDong.Biz
{
    public class KhoiLuongForDropDownList
    {
        public int Id { get; set; }
        public string DiemDau { get; set; }
        public string DiemCuoi { get; set; }
        public string MaTuyen { get; set; }
        public string MaTuyenExt
        {
            get { return string.Format("{0} ({1} - {2})", MaTuyen, DiemDau, DiemCuoi); }
        }
    }

    public class KhoiLuongBiz
    {
        #region Selection

        public List<KhoiLuongForDropDownList> GetKhoiLuongs(int hopDongId)
        {
            using (var db = new HopDongEntities())
            {
                return (from khoiluong in db.KhoiLuongHopDongViews
                    where khoiluong.HopDongId.Equals(hopDongId)
                    select new KhoiLuongForDropDownList
                    {
                        Id = khoiluong.Id,
                        MaTuyen = khoiluong.MaTuyen,
                        DiemDau = khoiluong.DiemDau,
                        DiemCuoi = khoiluong.DiemCuoi
                    })
                    //select khoiluong)
                    .ToList();
                //return db.KhoiLuongHopDongs.Where(x => x.HopDongId.Equals(hopDongId)).ToList();
            }
        }

        public List<KhoiLuongHopDongView> KhoiLuongList(int hopDongId)
        {
            using (var db = new HopDongEntities())
            {
                return db.KhoiLuongHopDongViews.Where(x => x.HopDongId.Equals(hopDongId)).ToList();
            }
        }

        public KhoiLuongHopDongView GetById(int id)
        {
            using (var db = new HopDongEntities())
            {
                return db.KhoiLuongHopDongViews.FirstOrDefault(x => x.Id.Equals(id));
            }
        }

        public List<KhoiLuongHopDongView> SearchKhoiLuong(int startIndex, int pageSize, string sorting
            , string tenDonVi, string soHopDong, int tinhTrangThiCongId, int tinhTrangVatTuId, string matram
            , Employee employee, int doiThiCongId, int trangThaiHopDongId
            , out int totalRecords)
        {
            using (var db = new HopDongEntities())
            {
                var query = from item in db.KhoiLuongHopDongViews select item;

                // filter
                if (tinhTrangThiCongId > 0)
                {
                    query = query.Where(x => x.TinhTrangThiCongId == tinhTrangThiCongId);
                }
                query = trangThaiHopDongId == 0 ? query.Where(x => x.TrangThai != 5) : query.Where(x => x.TrangThai == trangThaiHopDongId);
                if (tinhTrangVatTuId > 0)
                {
                    query = query.Where(x => x.TinhTrangVatTuId == tinhTrangVatTuId);
                }
                if (doiThiCongId > 0)
                {
                    query = query.Where(x => x.DoiThiCongId == doiThiCongId);
                }
                if (!string.IsNullOrEmpty(tenDonVi))
                {
                    query = query.Where(x => x.TenDonVi.Contains(tenDonVi));
                }
                if (!string.IsNullOrEmpty(soHopDong))
                {
                    query = query.Where(x => x.SoHopDong.Contains(soHopDong));
                }
                if (!string.IsNullOrEmpty(matram))
                {
                    query = query.Where(x => x.DiemDau.Contains(matram) || x.DiemCuoi.Contains(matram));
                }

                // count
                totalRecords = query.Count();

                return pageSize > 0
                           ? query.OrderBy(sorting).Skip(startIndex).Take(pageSize).ToList() //Paging
                           : query.ToList(); //No paging
            }
        }

        public List<KhoiLuongHopDongView> ExportKhoiLuongs(string tenDonVi, string soHopDong, int tinhTrangThiCongId, int tinhTrangVatTuId, string matram, int doiThiCongId, int trangThaiHopDongId)
        {
            using (var db = new HopDongEntities())
            {
                var query = from item in db.KhoiLuongHopDongViews select item;

                query = trangThaiHopDongId == 0 ? query.Where(x => x.TrangThai != 5) : query.Where(x => x.TrangThai == trangThaiHopDongId);
                if (tinhTrangVatTuId > 0)
                {
                    query = query.Where(x => x.TinhTrangVatTuId == tinhTrangVatTuId);
                }
                if (doiThiCongId > 0)
                {
                    query = query.Where(x => x.DoiThiCongId == doiThiCongId);
                }
                if (!string.IsNullOrEmpty(tenDonVi))
                {
                    query = query.Where(x => x.TenDonVi.Contains(tenDonVi));
                }
                if (!string.IsNullOrEmpty(soHopDong))
                {
                    query = query.Where(x => x.SoHopDong.Contains(soHopDong));
                }
                if (tinhTrangThiCongId > 0)
                {
                    query = query.Where(x => x.TinhTrangThiCongId == tinhTrangThiCongId);
                }
                if (!string.IsNullOrEmpty(matram))
                {
                    query = query.Where(x => x.DiemDau.Contains(matram) || x.DiemCuoi.Contains(matram));
                }

                return query.ToList();
            }
        }

        #endregion

        #region CRUD

        public void CreateKhoiLuong(KhoiLuongHopDong khoiLuongHopDong)
        {
            using (var db = new HopDongEntities())
            {
                // Diem dau
                var diemDau = db.DiaDiems.FirstOrDefault(x => x.Ma.Equals(khoiLuongHopDong.DiemDau));
                if (diemDau == null)
                {
                    // new
                    var newDiaDiem = new DiaDiem { Ma = khoiLuongHopDong.DiemDau };
                    db.DiaDiems.Add(newDiaDiem);
                    db.SaveChanges();

                    khoiLuongHopDong.DiemDauId = newDiaDiem.Id;
                }
                else // existing
                {
                    khoiLuongHopDong.DiemDauId = diemDau.Id;
                }

                // Diem cuoi
                var diemCuoi = db.DiaDiems.FirstOrDefault(x => x.Ma.Equals(khoiLuongHopDong.DiemCuoi));
                if (diemCuoi == null)
                {
                    // new
                    var newDiaDiem = new DiaDiem { Ma = khoiLuongHopDong.DiemCuoi };
                    db.DiaDiems.Add(newDiaDiem);
                    db.SaveChanges();

                    khoiLuongHopDong.DiemCuoiId = newDiaDiem.Id;
                }
                else // existing
                {
                    khoiLuongHopDong.DiemCuoiId = diemCuoi.Id;
                }

                // loai cap
                var loaiCap = db.LoaiCaps.FirstOrDefault(x => x.Name.Equals(khoiLuongHopDong.LoaiCap));
                if (loaiCap == null)
                {
                    // new
                    var newLoaiCap = new LoaiCap { Name = khoiLuongHopDong.LoaiCap };
                    db.LoaiCaps.Add(newLoaiCap);
                    db.SaveChanges();

                    khoiLuongHopDong.LoaiCapId = newLoaiCap.Id;
                }
                else // existing
                {
                    khoiLuongHopDong.LoaiCapId = loaiCap.Id;
                }

                // insert
                db.KhoiLuongHopDongs.Add(khoiLuongHopDong);
                db.SaveChanges();
                db.KiemTraThiCongXong(khoiLuongHopDong.HopDongId);
            }
        }

        public void UpdateKhoiLuong(KhoiLuongHopDong khoiLuongHopDong)
        {
            using (var db = new HopDongEntities())
            {
                // Diem dau
                var diemDau = db.DiaDiems.FirstOrDefault(x => x.Ma.Equals(khoiLuongHopDong.DiemDau));
                if (diemDau == null)
                {
                    // new
                    var newDiaDiem = new DiaDiem { Ma = khoiLuongHopDong.DiemDau };
                    db.DiaDiems.Add(newDiaDiem);
                    db.SaveChanges();

                    khoiLuongHopDong.DiemDauId = newDiaDiem.Id;
                }
                else // existing
                {
                    khoiLuongHopDong.DiemDauId = diemDau.Id;
                }

                // Diem cuoi
                var diemCuoi = db.DiaDiems.FirstOrDefault(x => x.Ma.Equals(khoiLuongHopDong.DiemCuoi));
                if (diemCuoi == null)
                {
                    // new
                    var newDiaDiem = new DiaDiem { Ma = khoiLuongHopDong.DiemCuoi };
                    db.DiaDiems.Add(newDiaDiem);
                    db.SaveChanges();

                    khoiLuongHopDong.DiemCuoiId = newDiaDiem.Id;
                }
                else // existing
                {
                    khoiLuongHopDong.DiemCuoiId = diemCuoi.Id;
                }

                // loai cap
                var loaiCap = db.LoaiCaps.FirstOrDefault(x => x.Name.Equals(khoiLuongHopDong.LoaiCap));
                if (loaiCap == null)
                {
                    // new
                    var newLoaiCap = new LoaiCap { Name = khoiLuongHopDong.LoaiCap };
                    db.LoaiCaps.Add(newLoaiCap);
                    db.SaveChanges();

                    khoiLuongHopDong.LoaiCapId = newLoaiCap.Id;
                }
                else // existing
                {
                    khoiLuongHopDong.LoaiCapId = loaiCap.Id;
                }

                // insert
                db.KhoiLuongHopDongs.Attach(khoiLuongHopDong);
                db.Entry(khoiLuongHopDong).State = EntityState.Modified;

                db.SaveChanges();

                db.KiemTraThiCongXong(khoiLuongHopDong.HopDongId);
            }
        }

        public void DeleteKhoiLuong(int id)
        {
            using (var db = new HopDongEntities())
            {
                db.DeleteKhoiLuong(id);
            }
        }

        #endregion
    }
}
